using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.IdentityModel.Tokens;
using NetTaste;
using SqlSugar;
using SqlSugar.Extensions;
using System.Drawing.Printing;
using System.Reflection;
using VM.BLL;
using VM.Common.DataUntil;
using VM.Common.EntityEnum;
using VM.Common.Result;
using VM.Entity.EntityDto;
using VM.Entity.EntityDto.Menu;
using VM.Entity.EntityMap.Member;
using VM.Entity.EntityMap.Menu;
using VM.Entity.EntityMap.System.Code;
using VM.IBLL;
using VM.IBLL.Menu;
using VM.IBLL.Token;
using VM.WebAPI.Controllers;
using VM.WebCore.Jwt;
using VM.WebCore.SwaggerExtend;
using VM.Wechat;

namespace WeatherForecastTest.Controllers
{
    /// <summary>
    ///菜单管理
    /// </summary>
    [ApiController]
    [Route("api/menu")]
    public class MenuController : ControllerBase
    {
        private readonly IMenuBLL _IMenuBLL;
        private readonly IJWTService _IJWTService;
        private readonly IHttpContextAccessor _HttpContextAccessor;
        public MenuController(IJWTService iJWTService, IMenuBLL iMenuBLL, IHttpContextAccessor httpContextAccessor)
        {

            _IMenuBLL = iMenuBLL;
            _IJWTService = iJWTService;
            _HttpContextAccessor = httpContextAccessor;
        }
        /// <summary>
        /// 获取菜单列表-根据用户授权 PC
        /// </summary>
        /// <param name="phone">手机号</param>
        /// <returns></returns>
        [HttpGet]
        [Authorize]//要求通过授权才行---只要能鉴权成功，都能通过]
        [Route("getMenuTreeList")]
        [ApiExplorerSettings(IgnoreApi = false, GroupName = nameof(ApiVersions.V1))]
        public ApiDataResult<List<getMenuTreeListDto>> getMenuTreeList(string? menuName)
        {
             
            //解析Jwt,获取到UserId
            int UserId = _IJWTService.GetUserIdByAuthInfoStr();
             
            //校验验证码是否可用，如果可用，返回true
            ApiDataResult<List<getMenuTreeListDto>> Res = _IMenuBLL.getMenuTreeList(Convert.ToInt32( UserId), menuName);
            return Res;
        }

        [HttpGet]
        [Authorize]//要求通过授权才行---只要能鉴权成功，都能通过]
        [Route("getMenuList")]
        [ApiExplorerSettings(IgnoreApi = false, GroupName = nameof(ApiVersions.V1))]
        public ApiDataResult<PagingData<getMenuListDto>> getMenuList(int pageIndex, int pageSize, string? menuName)
        {

            //解析Jwt,获取到UserId
            int UserId = _IJWTService.GetUserIdByAuthInfoStr();

            //校验验证码是否可用，如果可用，返回true
            ApiDataResult<PagingData<getMenuListDto>> Res = _IMenuBLL.getMenuList(pageIndex,pageSize,UserId, menuName);
            return Res;
        }

        /// <summary>
        /// 添加菜单 PC
        /// </summary>
        /// <param name="sys_member">登录信息</param>
        /// <returns></returns>
        [HttpPost]
        [Authorize]//要求通过授权才行---只要能鉴权成功，都能通过]
        [Route("addMenu")]
        [ApiExplorerSettings(IgnoreApi = false, GroupName = nameof(ApiVersions.V1))]
        public ApiResult addMenu(addMenuDto obj)
        {
            ApiResult  res = new ApiResult();

            //解析Jwt,获取到UserId
            int UserId = _IJWTService.GetUserIdByAuthInfoStr();
            //解析并优化类的传参
            obj = _IMenuBLL.perfectClass<addMenuDto>(obj, UserId, ProTypeEnum.save);
            return   _IMenuBLL.addMenu(obj);
  
                   
        }


        /// <summary>
        /// 根据menu.id获取菜单详细信息详情-需要鉴权 PC
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [Authorize]//要求通过授权才行---只要能鉴权成功，都能通过]
        [Route("getMenuInfoById")]
        [ApiExplorerSettings(IgnoreApi = false, GroupName = nameof(ApiVersions.V1))]
        public ApiDataResult<menuInfoDetailDto> getMenuInfoById(int menuId)
        {
            //解析Jwt,获取到UserId
            int userId = _IJWTService.GetUserIdByAuthInfoStr();
            //校验验证码是否可用，如果可用，返回true
            ApiDataResult<menuInfoDetailDto> Res = _IMenuBLL.getMenuInfoById(userId, menuId);
            return Res;
        }












    }
}